/*
 * Tint Browser for Android
 * 
 * Copyright (C) 2012 - to infinity and beyond J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package com.cnbrowser.android.ui.preferences;

import android.content.Context;
import android.content.SharedPreferences.Editor;
import android.preference.DialogPreference;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.cnbrowser.android.R;
import com.cnbrowser.android.utils.Constants;
import com.cnbrowser.android.utils.UrlUtils;

import java.util.HashMap;

public class SearchEnginePreference extends DialogPreference {


    private RadioGroup mGroup;

    private final HashMap<String, String> engineMap;


    public SearchEnginePreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        String[] keys = context.getResources().getStringArray(R.array.searchEngineNames);
        String[] values = context.getResources().getStringArray(R.array.searchEngineUrls);

        engineMap = new HashMap<String, String>();
        for (int i = 0; i < keys.length; i++) {
            engineMap.put(keys[i], values[i]);
        }
    }

    @Override
    protected View onCreateDialogView() {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        return inflater.inflate(R.layout.search_engine_preference, null);
    }

    @Override
    protected void onBindDialogView(View view) {
        super.onBindDialogView(view);
        mGroup = (RadioGroup) view.findViewById(R.id.radioGroupEngine);

        String currentUrl = UrlUtils.getRawSearchUrl(getContext());

        int index = 0;
        for (String key : engineMap.keySet()) {
            String url = engineMap.get(key);
            RadioButton radioButton = new RadioButton(getContext());
            if (url.equals(currentUrl)) {
                radioButton.setChecked(true);
            }
            radioButton.setText(key);
            radioButton.setId(index);
            mGroup.addView(radioButton, index);
            index++;
        }

    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);


        if (positiveResult) {
            Editor editor = PreferenceManager.getDefaultSharedPreferences(getContext()).edit();
            int radioButtonID = mGroup.getCheckedRadioButtonId();
            RadioButton radioButton = (RadioButton) mGroup.findViewById(radioButtonID);
            String key = (String) radioButton.getText();
            String url = engineMap.get(key);
            editor.putString(Constants.PREFERENCE_SEARCH_URL, url);
            editor.commit();
        }
    }

}
